<?php
declare (strict_types = 1);

namespace app\admin\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class SystemMenu extends Model
{
    protected $pk = "menu_id";

    //获取管理员的菜单
    function adminMenu($adminInfo){
        $getAdminMenuIdInfo = $this->getAdminMenuId($adminInfo);
        $where = ['is_show'=>1,'is_mer'=>1,'is_menu'=>1];
        //超级管理员
        if($getAdminMenuIdInfo['code'] == 0){
            $parentMenuList = $this->where($where)->where('pid',0)->select()->toArray();
            foreach ($parentMenuList as $ks=>$vs){
                $parentMenuList[$ks]['son_list'] = [];
            }
            $childrenMenuList = $this->where($where)->where('pid','>',0)->select()->toArray();
            foreach ($childrenMenuList as $key=>$value){
                foreach ($parentMenuList as $k=>$v){
                    if($value['pid'] == $v['menu_id']){
                        $parentMenuList[$k]['son_list'][] = $value;
                        break;
                    }
                }
            }
        }else{
            $adminMenuIdList = $getAdminMenuIdInfo['data'];
            $parentMenuList = $this->where($where)->where('pid',0)->select()->toArray();
            foreach ($parentMenuList as $key=>$value){
                if(!in_array($value['menu_id'],$adminMenuIdList)){
                    unset($parentMenuList[$key]);
                }
            }
            $childrenMenuList = $this->where($where)->where('pid','>',0)->select()->toArray();
            foreach ($childrenMenuList as $key=>$value){
                if(in_array($value['menu_id'],$adminMenuIdList)){
                    foreach ($parentMenuList as $k=>$v){
                        if($value['pid'] == $v['menu_id']){
                            $parentMenuList[$k]['son_list'][] = $value;
                            break;
                        }
                    }
                }
            }
        }
        return $parentMenuList;
    }

    /**
     * 获取用户权限id列表
     * @param $adminInfo
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getAdminMenuId($adminInfo){
        $adminRole = explode(",",$adminInfo['roles']);
        $allWhere = ['rules'=>'all'];
        $systemRoleModel = new SystemRole();
        $supperRoleId = $systemRoleModel->where($allWhere)->value('role_id');
        if(in_array($supperRoleId,$adminRole)){
            return ['code'=>0];
        }else{
            $roleList = $systemRoleModel->getRoleListByIdIn($adminInfo['roles']);
            $menuIdList = [];
            $menuWhere = ['is_show'=>1,'is_mer'=>1,'is_menu'=>1];
            foreach ($roleList as $key=>$value){
                $menuIdList = $this->whereIn('menu_id',$value['rules'])->where($menuWhere)->column('menu_id');
                $menuIdList = array_merge($menuIdList,$menuIdList);
            }
            return ['code'=>1,'data'=>$menuIdList];
        }
    }

    /**
     * 获取列表
     * @param $where
     * @param $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getList($where,$field){
        if(empty($where)){
            return $this->where('is_show',1)->field($field)->select()->toArray();
        }else{
            return $this->where($where)->where('is_show',1)->field($field)->select()->toArray();
        }

    }

    /**
     * 获取树状权限列表
     * @param array $selectIdList
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getTreeList($selectIdList = []){
        $where = ['is_show'=>1,'is_mer'=>1];
        $parentMenuList = $this->where($where)->where('pid',0)->select()->toArray();
        $treeMenuList = [];
        foreach ($parentMenuList as $key=>$value){
            if(in_array($value['menu_id'],$selectIdList)){
                $value['checked'] = 'checked';
            }else{
                $value['checked'] = '';
            }
            $treeMenuList[$value['menu_id']] = $value;
        }
        $childrenMenuList = $this->where($where)->where('pid','>',0)->select()->toArray();
        foreach ($childrenMenuList as $key=>$value){
            if(in_array($value['menu_id'],$selectIdList)){
                $value['checked'] = 'checked';
            }else{
                $value['checked'] = '';
            }
            $pathList = explode("/",$value['path']);
            if(count($pathList) == 2){
                $treeMenuList[$pathList[1]]['son_list'][$value['menu_id']] = $value;
            }else if(count($pathList) == 3){
                $treeMenuList[$pathList[1]]['son_list'][$pathList[2]]['son_list'][$value['menu_id']] = $value;
            }else if(count($pathList) == 4){
                $treeMenuList[$pathList[1]]['son_list'][$pathList[2]]['son_list'][$pathList[3]]['son_list'][$value['menu_id']] = $value;
            }
        }
        return $treeMenuList;

    }




}
